/**
 * Created by zyl on 17-5-24.
 */

import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

public class MyBC {

        private static Stack<String> stack;
        private final char ADD = '+';
        private final char SUBTRACT = '-';
        private final char MULTIPLY = '*';
        private final char DIVIDE = '/';

        public MyBC() {
            stack = new Stack<String>();
        }

        /*鍒ゆ柇琛ㄨ揪绗﹀彿*/
        public static boolean isOperator(String token) {
            return (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")||token.equals("(")||token.equals(")"));
        }

        /*鍒ゆ柇绗﹀彿浼樺厛绾??*/
        public static boolean charge(String item1,String item2){
        /*杩斿洖true琛ㄧず浼樺厛绾у悗鑰呴珮锛宖alse琛ㄧず浼樺厛绾у悗è呬綆*/
            if(item1.equals("+")||item1.equals("-")){
                if (item2.equals("+")||item2.equals("-")||item2.equals(")")||item2.equals("#"))
                    return false;
                else if(item2.equals("*")||item2.equals("/")||item2.equals("("))
                    return true;
            }
            else if(item1.equals("*")||item1.equals("/")){
                if(item2.equals("+")||item2.equals("-")||item2.equals(")")||item2.equals("#")||item2.equals("*")||item2.equals("/"))
                    return false;
                else if(item2.equals(item2.equals("(")))
                    return true;
            }
            else if(item1.equals("(")||item1.equals("#")){
                return true;
            }
            else if(item1.equals(")")){
                return false;
            }
            return false;
        }

        public int exchangexpr(String expr){

            String newexpr="";
            String token;
            String item1,item2;
            int op;
            int result;


        /*閫愪釜瀛楃璇诲彇expr*/
            StringTokenizer tokenizer = new StringTokenizer(expr);
            stack.push("#");

            while (tokenizer.hasMoreTokens()) {
                token = tokenizer.nextToken();

                if(isOperator(token)){/*杈撳叆涓烘搷浣滅*/
                    item1 = stack.pop();
                    if(charge(item1,token)){
                        stack.push(item1);
                        stack.push(token);
                    }else if(token == ")"){

                        while (item1 != "("){
                            newexpr = newexpr + item1 + " ";
                            item1 = stack.pop();
                        }
                        newexpr = newexpr + item1 + " ";

                    }else {
                        newexpr = newexpr + item1 + " ";
                    }
                }else {/*鐩存帴杈撳嚭鎿嶄綔鏁??*/
                    newexpr = newexpr + token + " ";
                }
            }


            System.out.println("the new expression is:" + newexpr);

        /*璋冪敤MyDC璁＄畻鍚庣紑琛ㄨ揪寮??*/
            MyDC evaluator = new MyDC();
            result = evaluator.evaluate(newexpr);

            return result;

        }

    }
